<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;
class TemplateRecycle extends \app\admin\controller\Admin{
    public function __construct(\think\App $app) {
        parent::__construct($app);
        $this->model=new \app\admin\model\TemplateRecycleModel();
        $this->templateModel=new \app\admin\model\TemplateModel();
        $this->TemplateHistoryModel=new \app\admin\model\TemplateHistoryModel();
    }
    public function index($type){
        $data=$this->model->where("type",$type)->order("delete_time desc")->select();
        View::assign("data",$data);
        return View::fetch();
    }
    public function view($template_id){        
        $r=$this->model->find($template_id);
        View::assign("r",$r);
        return View::fetch();
    }  
    public function recovery($template_id){
        $template=$this->model->find($template_id)->toArray();
        //删除当前表数据
        $this->model->where("template_id",$template_id)->delete();
        //删除delete_time字段
        unset($template["delete_time"]);
        $this->templateModel->replace()->save($template);
        //生成模板文件
        $this->templateModel->makeTemplateFile($template_id);
        //
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$template_id,"恢复模板");
        $this->success("恢复成功");    
    }
    //删除模板
    public function delete($template_id){
        //删除模板表记录
        $this->model->where("template_id",$template_id)->delete();
        //删除历吏记录
        $this->TemplateHistoryModel->where(array("template_id"=>$template_id))->delete();
        //删除文件
        $this->templateModel->deleteTemplateFile($template_id);
        //
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$template_id,$this->request->param());
        $this->success("删除成功");            
    } 
}